﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tank
{
    public partial class Form1 : Form
    {
        Thread p1;
       static Graphics windowG;
        static Bitmap map;
        public Form1()
        {
            InitializeComponent();
            // 创建一张600，600像素大小的图片
            map = new Bitmap(800, 800);
            // 创建图片画布
           Graphics bmg = Graphics.FromImage(map);
            // 创建画布
            windowG = this.CreateGraphics();
            GameFrameWorl.g = bmg;
            // 将窗口放置于中心
            this.StartPosition = FormStartPosition.CenterScreen;
            //线程p1
             p1 = new Thread(new ThreadStart(GameFrameWorkThread));
            p1.Start();
        }
        public static void GameFrameWorkThread()
        {

            GameFrameWorl.Start();
            // 每一帧都要检测，执行
            while (true)
            {
                // 使用黑色将画布进行填充
                //windowG.Clear(Color.Black);
                // 图片画布照样拥有Clear属性
                GameFrameWorl.g.Clear(Color.Black);
                // 计算时间差
                DateTime BeginTime = DateTime.Now;
                GameFrameWorl.Update();
                windowG.DrawImage(map,0,0);
                DateTime EndTime = DateTime.Now;
                TimeSpan day = EndTime.Subtract(BeginTime).Duration();
                int abs = day.Milliseconds;

                if (abs <= 33)
                {
                    Thread.Sleep(33 - abs);
                  
                }
            }
        }
        // 应该运行接近30贞
        //public static int Fps()
        //{
        //    // 计算时间差
        //    DateTime BeginTime = DateTime.Now;
        //    for (int i = 0; i < 1000000; i++) { }
        //    DateTime EndTime = DateTime.Now;
        //    TimeSpan day = EndTime.Subtract(BeginTime).Duration();
        //    //返回一个时间戳，或者时间间隔，.Duration将值进行一个绝对化处理；
        //    // TimeSpan day = EndTime.Subtract(BeginTime).Duration();
        //    int a = day.Milliseconds;
        //    Console.WriteLine("结果：{0}", a);

        //    return 0;
        //}

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            p1.Abort();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // sebder 
            GameObjectManager.KeyDown(e);
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            GameObjectManager.KeyUp(e);
        }
    }
}
